package com.gz.WeChatSys.runner;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMenuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpMenuServiceImpl;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


@Component
public class MenuCommandLineRunner implements CommandLineRunner {

    @Value("${wx.appId}")
    private String appId;

    @Value("${wx.secret}")
    private String secret;

    @Value("${wx.aesKey}")
    private String aesKey;


    @Override
    public void run(String... args) throws Exception {

        wxMpMenuService();
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpInMemoryConfigStorage.setAppId(appId);
        wxMpInMemoryConfigStorage.setSecret(secret);
        wxMpInMemoryConfigStorage.setAesKey(aesKey);
        return wxMpInMemoryConfigStorage;
    }
    @Bean
    public WxMpMenuService wxMpMenuService() throws WxErrorException {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        WxMpMenuService wxMpMenuService = new WxMpMenuServiceImpl(wxMpService);
        wxMpMenuService.menuCreate("{\"button\":[{\"type\":\"click\",\"name\":\"客服指南\",\"key\":\"kfzn\"},{\"type\":\"view\",\"name\":\"办事大厅\",\"url\":\"https://www.baidu.com\"}]}");
        return  wxMpMenuService;
    }
}
